<?php 
class ControllerToolLivechat extends AdminController {
	public function index() {
        M('tool/qq');
        $this->document->setTitle($this->_['heading_title']);
        $this->getList($this->model_tool_qq->getTotalQQ());
	}

    public function getList($query){  //$qurey是二维数组
        $this->data['qqs']=null;
        foreach ($query->rows as $row){
            $action = array();
            $action[] = array(
                'text' => $this->_['text_edit'],
                'href' => UA('tool/livechat/updateqq', 'qqNumber=' . $row['qqNumber'])
            );
            $row['imgName']='http://localhost/shop/themes/pavstore/image/kf/pa.gif';
            $row['action'] = $action;

            $this->data['qqs'][]=$row;
        }
        $this->display('tool/livechat_list.tpl');
    }

    public function insertqq() {
        M('tool/qq');
        if ($this->request->isPost()) {
            $this->model_tool_qq->addQQ($this->request->post);
            $this->session->set_flashdata('success', $this->_['text_success']);
            $this->redirect(UA('tool/livechat'));
        }
        $this->data['action'] = UA('tool/livechat/insertqq');
        $this->getForm();
    }

    public function getForm($form=null){
        $this->document->setTitle($this->_['heading_title']);
        if($form===null){
            $this->data['qqName'] = '';
            $this->data['qqStatus'] = 1;
            $this->data['qqNumber']='';
        }
        else {
            $this->data['qqName'] = $form['qqName'];
                $this->data['qqNumber']=$form['qqNumber'];
                $this->data['qqStatus'] = $form['qqStatus'];
        }
        $this->display('tool/qq_form.tpl');
    }

    public function updateqq() {
        M('tool/qq');
        $qqNumber = $this->request->get['qqNumber'];
        if ($this->request->isPost()) {
            $this->model_tool_qq->update( $this->request->post,$qqNumber);
            $this->session->set_flashdata('success', $this->_['text_success']);
            $this->redirect(UA('tool/livechat'));
        }
        $this->data['action'] = UA('tool/livechat/updateqq', "qqNumber=$qqNumber");
        $this->getForm($this->model_tool_qq->getQQ($qqNumber));
    }

    public function deleteqq() {
        M('tool/qq');
        if (!empty($this->request->post['selecte'])) {
            $this->model_tool_qq->delete($this->request->post['selecte']);
            $this->session->set_flashdata('success', $this->_['text_success']);
        }
        $this->redirect(UA('tool/livechat'));
    }

    public function launchqq(){
        M('tool/qq');
        if(!empty($this->request->post['selecte'])){
            foreach($this->request->post['selecte'] as $k=>$qqNumber){
            $this->model_tool_qq->changeStatus($qqNumber);
            }
            $this->session->set_flashdata('success', $this->_['text_success']);
        }
        $this->redirect(UA('tool/livechat'));
    }

}